package com.ruoyi.shopping.service.impl;


import java.util.*;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.shopping.domain.Commodity;
import com.ruoyi.shopping.mapper.CommodityMapper;
import com.ruoyi.shopping.service.ICommodityService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 商品管理Service业务层处理
 *
 * @author ysq
 * @date 2024-08-14
 */
@Service
public class CommodityServiceImpl implements ICommodityService {
    @Resource
    private CommodityMapper commodityMapper;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 查询商品管理
     *
     * @param id 商品管理主键
     * @return 商品管理
     */
    @Override
    public Commodity selectCommodityById(Long id) {
        return commodityMapper.selectCommodityById(id);
    }

    /**
     * 查询商品管理列表
     *
     * @param commodity 商品管理
     * @return 商品管理
     */
    @Override
    public List<Commodity> selectCommodityList(Commodity commodity) {
        return commodityMapper.selectCommodityList(commodity);
    }

    /**
     * 新增商品管理
     *
     * @param commodity 商品管理
     * @return 结果
     */
    @Override
    public int insertCommodity(Commodity commodity) {
        return commodityMapper.insertCommodity(commodity);
    }

    /**
     * 修改商品管理
     *
     * @param commodity 商品管理
     * @return 结果
     */
    @Override
    public int updateCommodity(Commodity commodity) {
        return commodityMapper.updateCommodity(commodity);
    }

    /**
     * 批量删除商品管理
     *
     * @param ids 需要删除的商品管理主键
     * @return 结果
     */
    @Override
    public int deleteCommodityByIds(Long[] ids) {
        return commodityMapper.deleteCommodityByIds(ids);
    }

    /**
     * 删除商品管理信息
     *
     * @param id 商品管理主键
     * @return 结果
     */
    @Override
    public int deleteCommodityById(Long id) {
        return commodityMapper.deleteCommodityById(id);
    }

    /**
     * 热门数据的获取
     *
     * @return 结果
     */
    @Override
    public List<Commodity> getHotCommodity() {
        Set<ZSetOperations.TypedTuple<Object>> set = redisTemplate.opsForZSet().reverseRangeWithScores("hotCommodity", 0, -1);
        System.out.println(set.toString());
        // 查询数据库
        List<Commodity> commodityList;
        if (set.isEmpty()) {
            QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
            QueryWrapper<Commodity> sales = queryWrapper.orderByDesc("sales");
            /*queryWrapper.last("LIMIT 3");  // 限制结果为前三条*/
            commodityList = commodityMapper.selectList(sales);
            System.out.println(commodityList.toString());
            for (Commodity commodity : commodityList) {
                redisTemplate.opsForZSet().add("hotCommodity", commodity, commodity.getSales());
            }
            return commodityList;
        }

        commodityList = new ArrayList<>();
        set.forEach(item -> {
            Object hotIdStr = item.getValue();
            commodityList.add((Commodity) hotIdStr);
        });
        return commodityList;
    }

    /**
     * 热门数据的获取(限制三条)
     *
     * @return 结果
     */
    @Override
    public List<Commodity> getHotLimitCommodity() {
        Set<ZSetOperations.TypedTuple<Object>> set = redisTemplate.opsForZSet().reverseRangeWithScores("hotLimitCommodity", 0, -1);
        System.out.println(set.toString());
        // 查询数据库
        List<Commodity> commodityList;
        if (set.isEmpty()) {
            QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
            QueryWrapper<Commodity> sales = queryWrapper.orderByDesc("sales");
            queryWrapper.last("LIMIT 3");  // 限制结果为前三条
            commodityList = commodityMapper.selectList(sales);
            System.out.println(commodityList.toString());
            for (Commodity commodity : commodityList) {
                redisTemplate.opsForZSet().add("hotLimitCommodity", commodity, commodity.getSales());
            }
            return commodityList;
        }

        commodityList = new ArrayList<>();
        set.forEach(item -> {
            Object hotIdStr = item.getValue();
            commodityList.add((Commodity) hotIdStr);
        });
        return commodityList;
    }

    /**
     * 新品数据的获取(3条)
     *
     * @return 结果
     */
    @Override
    public List<Commodity> getNewLimitCommodity() {
        Set<ZSetOperations.TypedTuple<Object>> set = redisTemplate.opsForZSet().reverseRangeWithScores("newLimitCommodity", 0, -1);
        // 查询数据库
        List<Commodity> commodityList;
        if (set.isEmpty()) {
            QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
            QueryWrapper<Commodity> id = queryWrapper.orderByDesc("id");
            queryWrapper.last("LIMIT 3");  // 限制结果为前三条
            commodityList = commodityMapper.selectList(id);
            System.out.println(commodityList.toString());
            for (Commodity commodity : commodityList) {
                redisTemplate.opsForZSet().add("newLimitCommodity", commodity, commodity.getId());
            }
            return commodityList;
        }

        commodityList = new ArrayList<>();
        set.forEach(item -> {
            Object hotIdStr = item.getValue();
            commodityList.add((Commodity) hotIdStr);
        });
        return commodityList;
    }
    /**
     * 新品数据的获取
     *
     * @return 结果
     */
    @Override
    public List<Commodity> getNewCommodity() {
        Set<ZSetOperations.TypedTuple<Object>> set = redisTemplate.opsForZSet().reverseRangeWithScores("newCommodity", 0, -1);
        // 查询数据库
        List<Commodity> commodityList;
        if (set.isEmpty()) {
            QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
            QueryWrapper<Commodity> id = queryWrapper.orderByDesc("id");
            commodityList = commodityMapper.selectList(id);
            System.out.println(commodityList.toString());
            for (Commodity commodity : commodityList) {
                redisTemplate.opsForZSet().add("newCommodity", commodity, commodity.getId());
            }
            return commodityList;
        }

        commodityList = new ArrayList<>();
        set.forEach(item -> {
            Object hotIdStr = item.getValue();
            commodityList.add((Commodity) hotIdStr);
        });
        return commodityList;
    }

    @Override
    public int purchase(Long id, Integer count) {
        Map<String,Object> map = new HashMap<>();
        map.put("id", id);
        map.put("count", count);
        return commodityMapper.purchase(map);
    }

    @Override
    public List<Commodity> selectSearch(String description) {
        return commodityMapper.selectSearch(description);
    }
    @Override
    public List<Commodity> hotSearch() {
        return commodityMapper.hotSearch();
    }
}
